package net.javalib.batchmkv.script;

import java.io.File;

import org.apache.log4j.Logger;

import bsh.Interpreter;

public class ScriptRunner {
	private static final Logger logger = Logger.getLogger(ScriptRunner.class);
	
	public static void main(String[] args) {
		String script = "convert";
		if (args.length > 0)
			script = args[0];
		try {
			run(script);
		} catch (Exception e) {
			logger.error("Error running script: "+script,e);
		}
	}

	private static void run(String script) throws Exception {
		// Locate script
		File scriptFile = new File(script);
		if (!scriptFile.exists()) {
			scriptFile = new File("scripts/"+script);
			if (!scriptFile.exists()) {
				if (!script.endsWith(".bsh"))
					run(script+".bsh");
				else 
					logger.error("Unable to locate script: "+script);
			}
		}
		if (scriptFile.exists()) {
			logger.info("running script: "+scriptFile.getAbsolutePath());
			new Interpreter().source(scriptFile.getAbsolutePath());
		}
	}
	
}
